﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using hou_elearning.Entity;
using hou_elearning.Business;

public partial class Admin_control_ucBientapSlide : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Cấu hình CFKEditor
        initCFKEditor();
        if (!IsPostBack)
        {
            //Hiển thị danh sách trang slide
            napRptListSlide();
            //Nạp dropDowList danh mục sự kiện
            napDdlSukien();
        }
    }

    /// <summary>
    /// Hàm cấu hình CFKEditor
    /// </summary>
    /// <author>liemqv</author>
    /// <create>22/05/2012</create>
    /// 
    public void initCFKEditor()
    {
        cfkEdit.Language = "vi";
        //CKEditorControl1.config.filebrowserImageBrowseUrl = "/ckfinder/ckfinder.html?type=Images";
        //CKEditorControl1.config.filebrowserImageUploadUrl = "/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images";
        CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
        _FileBrowser.BasePath = "/hou-elearning/ckfinder/";
        _FileBrowser.SetupCKEditor(cfkEdit);
    }

    /// <summary>
    /// Hàm nạp dữ liệu cho rptListSlide
    /// </summary>
    /// <author>liemqv</author>
    /// <create>21/05/2012</create>
    /// 
    public void napRptListSlide()
    {
        List<TrangslideEntity> lstTrangslide = TrangslideBRL.GetByFK_iSlideID(getPK_iSlideID());
        rptListSlide.DataSource = lstTrangslide;
        rptListSlide.DataBind();
        if(lstTrangslide.Count > 0)
        {
            Session["selectdSlide"] = lstTrangslide[0].PK_iTrangslideID;
            displaySelectedPage(lstTrangslide[0].PK_iTrangslideID);
        }
        else
        {
            Session["selectdSlide"] = 0;
        }
    }

    /// <summary>
    /// Hàm lấy SlideID từ Session
    /// </summary>
    /// <author>liemqv</author>
    /// <create>21/05/2012</create>
    /// 
    public Int64 getPK_iSlideID()
    {
        Int64 iRes = 0;
        if (Session["PK_iSlideID"] != null)
        {
            iRes = Int64.Parse(Session["PK_iSlideID"].ToString());
        }
        else
        {
            Response.Redirect("~/Admin/?page=Slide");
        }
        return iRes;
    }
    /// <summary>
    /// Hàm lấy PK_iTrangslideID được người dùng chọn, đã lưu vào session
    /// </summary>
    /// <author>liemqv</author>
    /// <create>21/05/2012</create>
    /// 
    public Int64 getiSelectedSlide()
    {
        Int64 iRes = 0;
        if (Session["selectdSlide"] != null)
        {
            iRes = Int64.Parse(Session["selectdSlide"].ToString());
        }
        else
        {
            iRes = 0;
        }
        return iRes;
    }

    /// <summary>
    /// Lấy ID người dùng đăng nhập
    /// </summary>
    /// <author>liemqv</author>
    /// <create>25/04/2012</create>
    /// 
    public Int64 iGetUserLogin()
    {
        Int64 PK_iNguoidungID = 0;
        try
        {
            if (Session["UserID"] != null)
            {
                PK_iNguoidungID = Int64.Parse(Session["UserID"].ToString());
            }
        }
        catch (Exception ex)
        {
            PK_iNguoidungID = 0;
        }
        return PK_iNguoidungID;
    }

    protected void lbtnAddSlide_Click(object sender, EventArgs e)
    {
        panEditPageSlide.Visible = true;
        btnOK.CommandName = "AddSlide";
    }
    protected void lbtnEditSlide_Click(object sender, EventArgs e)
    {
        Int64 PK_iSlideID = getiSelectedSlide();
        SlideEntity oSlide = SlideBRL.GetOne(PK_iSlideID);
        txtTieude.Text = oSlide.sTenslide;
        panEditPageSlide.Visible = true;
    }
    protected void lbtnRemoveSlide_Click(object sender, EventArgs e)
    {
        Int64 PK_iTrangslideID = getiSelectedSlide();
        if(PK_iTrangslideID > 0)
        {
            if (TrangslideBRL.Remove(PK_iTrangslideID) == true)
            {
                divThongbao.InnerHtml = "<p class='info' id='success'><span class='info_inner'>Xóa trang slide thành công!</span></p>";
                napRptListSlide();
            }
            else
            {
                divThongbao.InnerHtml = "<p class='info' id='error'><span class='info_inner'>Xóa trang slide thất bại!</span></p>";
            }
        }
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        switch(btnOK.CommandName)
        {
            case "AddSlide":
                addSlide();
            break;
        }
    }
    /// <summary>
    /// Thêm trang slide mới
    /// </summary>
    /// <author>liemqv</author>
    /// <create>25/04/2012</create>
    ///
    public void addSlide()
    {
        TrangslideEntity oTrangslide = new TrangslideEntity();
        oTrangslide.FK_iSlideID = getPK_iSlideID();
        oTrangslide.sTieude = txtTieude.Text;
        oTrangslide.sGhichu = txtGhichu.Text;
        oTrangslide.iSothutu = short.Parse("0" + txtSothutu.Text);
        Int64 PK_iTrangslideID = TrangslideBRL.Add(oTrangslide);
        if(PK_iTrangslideID > 0)
        {
            divThongbao.InnerHtml = "<p class='info' id='success'><span class='info_inner'>Thêm trang slide thành công!</span></p>";
            Session["selectdSlide"] = PK_iTrangslideID;
            napRptListSlide();
            displaySelectedPage(PK_iTrangslideID);
        }
        else
        {
            divThongbao.InnerHtml = "<p class='info' id='error'><span class='info_inner'>Thêm trang slide thất bại!</span></p>";
        }
        resetFrom();
        panEditPageSlide.Visible = false;
    }
    /// <summary>
    /// Hiển thị trang slide được chọn theo PK_iTrangslideID
    /// </summary>
    /// <author>liemqv</author>
    /// <create>25/04/2012</create>
    ///
    public void displaySelectedPage(Int64 PK_iTrangslideID)
    {
        Session["selectdSlide"] = PK_iTrangslideID;
        List<SukienslideEntity> lstSukienslide = SukienslideBRL.GetByFK_iTrangslideID(PK_iTrangslideID);
        if(lstSukienslide.Count > 0)
        {
            rptListEvent.DataSource = lstSukienslide;
            rptListEvent.DataBind();
        }
        else
        {
            rptListEvent.DataSource = null;
            rptListEvent.DataBind();
            divViewslide.InnerHtml = "Chưa có nội dung";
        }
    }
    /// <summary>
    /// Đặt các text của form sự kiện slide về trạng thái ban đầu
    /// </summary>
    /// <author>liemqv</author>
    /// <create>26/04/2012</create>
    ///
    public void resetFromEvent()
    {
        cfkEdit.Text = "";
        txtSttSukienslide.Text = "1";
        txtThoigianhienthi.Text = "5";
        divThongbao.InnerHtml = "";
    }
    /// <summary>
    /// Đặt các text về trạng thái ban đầu
    /// </summary>
    /// <author>liemqv</author>
    /// <create>25/04/2012</create>
    ///
    public void resetFrom()
    {
        txtGhichu.Text = "";
        txtTieude.Text = "";
        txtSothutu.Text = "";
        cfkEdit.Text = "";
        divThongbao.InnerHtml = "";
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        resetFrom();
        panEditPageSlide.Visible = false;
    }
    protected void rptListSlide_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        LinkButton lbtnSelectSlide = (LinkButton)e.CommandSource;
        Int64 PK_iTrangslideID = Int64.Parse(lbtnSelectSlide.CommandArgument);
        displaySelectedPage(PK_iTrangslideID);
        Session["selectdSlide"] = PK_iTrangslideID;
    }
    protected void lbtnAddEvent_Click(object sender, EventArgs e)
    {
        resetFromEvent();
        panEditEventSlide.Visible = true;
        lbtnSukienslideOK.CommandName = "AddEvent";
    }
    protected void lbtnSukienslideOK_Click(object sender, EventArgs e)
    {
        Int64 PK_iSlideID = getiSelectedSlide();
        Int64 PK_iSukienslideID = 0;
        SukienslideEntity oSukienslide = new SukienslideEntity();
        String strCommandName = lbtnSukienslideOK.CommandName;
        oSukienslide.FK_iTrangslideID = PK_iSlideID;
        oSukienslide.FK_sDanhmucsukienbatdauID = ddlSukienbatdau.SelectedValue;
        oSukienslide.FK_sDanhmucsukienkethucID = ddlSukienketthuc.SelectedValue;
        oSukienslide.iSothutu = short.Parse(txtSttSukienslide.Text);
        oSukienslide.iThoigianhienthi = short.Parse(txtThoigianhienthi.Text);
        oSukienslide.sNoidung = cfkEdit.Text;
        switch(strCommandName)
        {
            case "AddEvent":
                if (SukienslideBRL.Add(oSukienslide) > 0)
                {
                    divThongbao.InnerHtml = "<p class='info' id='success'><span class='info_inner'>Thêm sự kiện slide thành công!</span></p>";
                    displaySelectedPage(getiSelectedSlide());
                }
                else
                {
                    divThongbao.InnerHtml = "<p class='info' id='error'><span class='info_inner'>Thêm sự kiện slide thất bại!</span></p>";
                }
                break;
            case "EditEvent":
                PK_iSukienslideID = Int64.Parse(lbtnSukienslideOK.CommandArgument);
                oSukienslide.PK_iSukienslideID = PK_iSukienslideID;
                if (SukienslideBRL.Edit(oSukienslide) == true)
                {
                    divThongbao.InnerHtml = "<p class='info' id='success'><span class='info_inner'>Sửa sự kiện slide thành công!</span></p>";
                    displaySelectedPage(getiSelectedSlide());
                }
                else
                {
                    divThongbao.InnerHtml = "<p class='info' id='error'><span class='info_inner'>Sửa sự kiện slide thất bại!</span></p>";
                }
                break;
        }
        /* Ẩn form cập nhật sự kiện slide */
        panEditEventSlide.Visible = false;
    }
    protected void lbtnSukienslideCancel_Click(object sender, EventArgs e)
    {
        resetFromEvent();
        panEditEventSlide.Visible = false;
    }
    protected void rptListEvent_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        LinkButton lbtnEditEvent = (LinkButton)e.CommandSource;
        Int64 PK_iSukienslideID = Int64.Parse(lbtnEditEvent.CommandArgument);
        String strCommandName = lbtnEditEvent.CommandName;
        switch(strCommandName)
        {
            case "editEvent":
                lbtnSukienslideOK.CommandArgument = PK_iSukienslideID.ToString(); //Gán ID của sự kiện cho lbtnSukienslideOK phục vụ cho việc lấy lại ID
                editEvent(PK_iSukienslideID);
                break;
            case "deleteEvent":
                lbtnSukienslideOK.CommandArgument = PK_iSukienslideID.ToString();
                deleteEvent(PK_iSukienslideID);
                break;
        }
    }

    /// <summary>
    /// Hiện thông tin sự kiện slide hiện tại để phục vụ cho việc sửa
    /// </summary>
    /// <author>liemqv</author>
    /// <create>26/04/2012</create>
    /// <param name="PK_iSukienslideID">ID của sự kiện slide</param>
    ///
    public void editEvent(Int64 PK_iSukienslideID)
    {
        SukienslideEntity oSukienslide = SukienslideBRL.GetOne(PK_iSukienslideID);
        txtSttSukienslide.Text = oSukienslide.iSothutu.ToString();
        cfkEdit.Text = oSukienslide.sNoidung;
        txtThoigianhienthi.Text = oSukienslide.iThoigianhienthi.ToString();
        ddlSukienbatdau.SelectedItem.Selected = false;
        ddlSukienketthuc.SelectedItem.Selected = false;
        ddlSukienbatdau.Items.FindByValue(oSukienslide.FK_sDanhmucsukienbatdauID).Selected = true;
        ddlSukienketthuc.Items.FindByValue(oSukienslide.FK_sDanhmucsukienkethucID).Selected = true;
        lbtnSukienslideOK.CommandName = "EditEvent";
        panEditEventSlide.Visible = true;
    }

    /// <summary>
    /// Xóa sự kiện slide theo ID
    /// </summary>
    /// <author>liemqv</author>
    /// <create>26/04/2012</create>
    /// <param name="PK_iSukienslideID">ID của sự kiện slide</param>
    ///
    public void deleteEvent(Int64 PK_iSukienslideID)
    {
        if(SukienslideBRL.Remove(PK_iSukienslideID) == true)
        {
            divThongbao.InnerHtml = "<p class='info' id='success'><span class='info_inner'>Xóa sự kiện slide thành công!</span></p>";
            displaySelectedPage(getiSelectedSlide());
        }
        else
        {
            divThongbao.InnerHtml = "<p class='info' id='error'><span class='info_inner'>Xóa sự kiện slide thất bại!</span></p>";
        }
    }
    /// <summary>
    /// Nạp DropDownList ddlSukienbatdau
    /// </summary>
    /// <author>liemqv</author>
    /// <create>26/04/2012</create>
    ///
    public void napDdlSukien()
    {
        List<DanhmucsukienslideEntity> lstDanhmucsukienslide = DanhmucsukienslideBRL.GetAll();
        lstDanhmucsukienslide.Reverse();
        ddlSukienbatdau.DataSource = lstDanhmucsukienslide;
        ddlSukienbatdau.DataTextField = "sTensukien";
        ddlSukienbatdau.DataValueField = "PK_sDanhmucsukienslideID";
        ddlSukienbatdau.DataBind();

        ddlSukienketthuc.DataSource = lstDanhmucsukienslide;
        ddlSukienketthuc.DataTextField = "sTensukien";
        ddlSukienketthuc.DataValueField = "PK_sDanhmucsukienslideID";
        ddlSukienketthuc.DataBind();
    }
}